package cn.bjxy.shishaolong.request_answer.controller;

import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.request_answer.entity.Answer;
import cn.bjxy.shishaolong.request_answer.entity.Request;
import cn.bjxy.shishaolong.request_answer.service.IAnswerService;
import cn.bjxy.shishaolong.request_answer.service.IRequestService;
import cn.bjxy.shishaolong.system.controller.AbstractController;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author 时少龙
 * @Date 2019-04-13 10:00
 * @Version 1.0
 */
public class RequestAnswerBaseController extends AbstractController {

    @Autowired
    protected IRequestService requestService;
    @Autowired
    protected IAnswerService answerService;

    public static final String hasAdopt = "问题已完结（已有采纳答案）";

    /**
     * 判断用户是否为提问者本人
     *
     * @param userId     用户id
     * @param requestTid 问题id
     * @return 是返回提问者所提问题，否：返回null
     */
    protected Request isRequestorOwn(Integer userId, Integer requestTid) {
        Request retRequest = requestService.getOne(new QueryWrapper<Request>().lambda()
                .eq(Request::getUserId, userId)
                .eq(Request::getTid, requestTid));
        return retRequest;
    }

    /**
     * 判断问题是否已采纳
     *
     * @param requestTid 问题id
     * @return 是true，否：返回false
     */
    protected boolean isAdopt(Integer requestTid) {
        Request request = requestService.getById(requestTid);
        if (request == null) {
            throw new OperatorFailureException(-1, "所要回复的问题不存在");
        }
        return request.getBestAnswerId() == -1 ? false : true;
    }

    /**
     * 判断用户是否为回答者本人
     *
     * @param userId   用户id
     * @param answerId 问题id
     * @return 是返回提问者所提问题，否：返回null
     */
    protected Answer isAnswererOwn(Integer userId, Integer answerId) {
        Answer retAnswer = answerService.getOne(new QueryWrapper<Answer>().lambda()
                .eq(Answer::getTid, userId).eq(Answer::getTid, answerId));
        return retAnswer;
    }

    /**
     * 判断用户是否可以再追问再次回复
     *
     * @param userId
     * @return
     */
    protected boolean canReply(Integer answerId, Integer userId) {
        List<Answer> attachList = getAttachListByAnswerId(answerId);
        if (attachList == null || attachList.size() == 0) return false;
        Answer answer = attachList.get(attachList.size() - 1);
        if (userId == answer.getUserId()) return false;
        return true;
    }

    /**
     * 通过回答id获取在追问再回答列表
     *
     * @param answerId
     * @return
     */
    protected List<Answer> getAttachListByAnswerId(Integer answerId) {
        List<Answer> list = answerService.list(new QueryWrapper<Answer>().lambda()
                .eq(Answer::getPid, answerId).isNull(Answer::getDeletedAt));
        return list;
    }

    /*================================问题回答通用返回实体==================================*/

    /**
     * 返回问题已完结
     *
     * @return
     */
    protected ResultMsg retHasAdopt() {
        return ResultMsg.create(-1, hasAdopt);
    }

    /**
     * 非本人操作
     */
    protected ResultMsg retNotOwnOperator() {
        return ResultMsg.create(StatusCode.NOT_OWN_OPERATION, StatusCode.NOT_OWN_OPERATION_MSG);
    }
}
